﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract Rabat typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class RabatDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<RabatDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Rabat _currentRabat = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxRabatDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxRabatErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxRabatBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDKlijent property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDKlijentDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDProizvodac property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDProizvodacDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDKategorija property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDKategorijaDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Vrijednost property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxVrijednostDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.ArtiklKategorija> _IDKategorijaList;
		
		/// <summary> 
		/// The list of selectable ArtiklKategorija
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.ArtiklKategorija> IDKategorijaList
		{
			get {return this._IDKategorijaList;}
			set 
			{
				this._IDKategorijaList = value;
				this.uxIDKategorijaDataGridViewColumn.DataSource = null;
				this.uxIDKategorijaDataGridViewColumn.DataSource = this._IDKategorijaList;
			}
		}
		
		private bool _allowNewItemInIDKategorijaList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of ArtiklKategorija
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDKategorijaList
		{
			get { return _allowNewItemInIDKategorijaList;}
			set
			{
				this._allowNewItemInIDKategorijaList = value;
				this.uxIDKategorijaDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Klijent> _IDKlijentList;
		
		/// <summary> 
		/// The list of selectable Klijent
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Klijent> IDKlijentList
		{
			get {return this._IDKlijentList;}
			set 
			{
				this._IDKlijentList = value;
				this.uxIDKlijentDataGridViewColumn.DataSource = null;
				this.uxIDKlijentDataGridViewColumn.DataSource = this._IDKlijentList;
			}
		}
		
		private bool _allowNewItemInIDKlijentList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Klijent
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDKlijentList
		{
			get { return _allowNewItemInIDKlijentList;}
			set
			{
				this._allowNewItemInIDKlijentList = value;
				this.uxIDKlijentDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Klijent> _IDProizvodacList;
		
		/// <summary> 
		/// The list of selectable Klijent
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Klijent> IDProizvodacList
		{
			get {return this._IDProizvodacList;}
			set 
			{
				this._IDProizvodacList = value;
				this.uxIDProizvodacDataGridViewColumn.DataSource = null;
				this.uxIDProizvodacDataGridViewColumn.DataSource = this._IDProizvodacList;
			}
		}
		
		private bool _allowNewItemInIDProizvodacList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Klijent
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDProizvodacList
		{
			get { return _allowNewItemInIDProizvodacList;}
			set
			{
				this._allowNewItemInIDProizvodacList = value;
				this.uxIDProizvodacDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Rabat> _RabatList;
				
		/// <summary> 
		/// The list of Rabat to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Rabat> RabatList
		{
			get {return this._RabatList;}
			set
			{
				this._RabatList = value;
				this.uxRabatBindingSource.DataSource = null;
				this.uxRabatBindingSource.DataSource = value;
				this.uxRabatDataGridView.DataSource = null;
				this.uxRabatDataGridView.DataSource = this.uxRabatBindingSource;				
				//this.uxRabatBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxRabatBindingSource_ListChanged);
				this.uxRabatBindingSource.CurrentItemChanged += new System.EventHandler(OnRabatBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnRabatBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentRabat = uxRabatBindingSource.Current as Entities.Rabat;
			
			if (_currentRabat != null)
			{
				_currentRabat.Validate();
			}
			//_Rabat.Validate();
			OnCurrentEntityChanged();
		}

		//void uxRabatBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Rabat"/> instance.
		/// </summary>
		public Entities.Rabat SelectedRabat
		{
			get {return this._currentRabat;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxRabatDataGridView.VirtualMode;}
			set
			{
				this.uxRabatDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxRabatDataGridView.AllowUserToAddRows;}
			set {this.uxRabatDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxRabatDataGridView.AllowUserToDeleteRows;}
			set {this.uxRabatDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxRabatDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxRabatDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="RabatDataGridViewBase"/> class.
		/// </summary>
		public RabatDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxRabatDataGridView = new System.Windows.Forms.DataGridView();
			this.uxRabatBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxRabatErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDKlijentDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDProizvodacDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDKategorijaDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxVrijednostDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDKategorijaBindingSource = new ArtiklKategorijaBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKategorijaBindingSource)).BeginInit();
			//this.uxIDKlijentBindingSource = new KlijentBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKlijentBindingSource)).BeginInit();
			//this.uxIDProizvodacBindingSource = new KlijentBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDProizvodacBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxRabatErrorProvider
			// 
			this.uxRabatErrorProvider.ContainerControl = this;
			this.uxRabatErrorProvider.DataSource = this.uxRabatBindingSource;						
			// 
			// uxRabatDataGridView
			// 
			this.uxRabatDataGridView.AutoGenerateColumns = false;
			this.uxRabatDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxRabatDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxIDKlijentDataGridViewColumn,
		this.uxIDProizvodacDataGridViewColumn,
		this.uxIDKategorijaDataGridViewColumn,
		this.uxVrijednostDataGridViewColumn			});
			this.uxRabatDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxRabatDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxRabatDataGridView.Name = "uxRabatDataGridView";
			this.uxRabatDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxRabatDataGridView.TabIndex = 0;	
			this.uxRabatDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxRabatDataGridView.EnableHeadersVisualStyles = false;
			this.uxRabatDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnRabatDataGridViewDataError);
			this.uxRabatDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRabatDataGridViewCellValueNeeded);
			this.uxRabatDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRabatDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDKlijentDataGridViewColumn
			//
			this.uxIDKlijentDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDKlijentDataGridViewColumn.DataPropertyName = "IDKlijent";
			this.uxIDKlijentDataGridViewColumn.HeaderText = "IDKlijent";
			this.uxIDKlijentDataGridViewColumn.Name = "uxIDKlijentDataGridViewColumn";
			this.uxIDKlijentDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDKlijentDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDKlijentDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDProizvodacDataGridViewColumn
			//
			this.uxIDProizvodacDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDProizvodacDataGridViewColumn.DataPropertyName = "IDProizvodac";
			this.uxIDProizvodacDataGridViewColumn.HeaderText = "IDProizvodac";
			this.uxIDProizvodacDataGridViewColumn.Name = "uxIDProizvodacDataGridViewColumn";
			this.uxIDProizvodacDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDProizvodacDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDProizvodacDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDKategorijaDataGridViewColumn
			//
			this.uxIDKategorijaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDKategorijaDataGridViewColumn.DataPropertyName = "IDKategorija";
			this.uxIDKategorijaDataGridViewColumn.HeaderText = "IDKategorija";
			this.uxIDKategorijaDataGridViewColumn.Name = "uxIDKategorijaDataGridViewColumn";
			this.uxIDKategorijaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDKategorijaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDKategorijaDataGridViewColumn.ReadOnly = false;		
			//
			// uxVrijednostDataGridViewColumn
			//
			this.uxVrijednostDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxVrijednostDataGridViewColumn.DataPropertyName = "Vrijednost";
			this.uxVrijednostDataGridViewColumn.HeaderText = "Vrijednost";
			this.uxVrijednostDataGridViewColumn.Name = "uxVrijednostDataGridViewColumn";
			this.uxVrijednostDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxVrijednostDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxVrijednostDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDKategorijaDataGridViewColumn
			//				
			this.uxIDKategorijaDataGridViewColumn.DisplayMember = "Naziv";	
			this.uxIDKategorijaDataGridViewColumn.ValueMember = "ID";	
			this.uxIDKategorijaDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDKategorijaDataGridViewColumn.DataSource = uxIDKategorijaBindingSource;				
				
			//
			// uxIDKlijentDataGridViewColumn
			//				
			this.uxIDKlijentDataGridViewColumn.DisplayMember = "ImeKlijenta";	
			this.uxIDKlijentDataGridViewColumn.ValueMember = "ID";	
			this.uxIDKlijentDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDKlijentDataGridViewColumn.DataSource = uxIDKlijentBindingSource;				
				
			//
			// uxIDProizvodacDataGridViewColumn
			//				
			this.uxIDProizvodacDataGridViewColumn.DisplayMember = "ImeKlijenta";	
			this.uxIDProizvodacDataGridViewColumn.ValueMember = "ID";	
			this.uxIDProizvodacDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDProizvodacDataGridViewColumn.DataSource = uxIDProizvodacBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxRabatDataGridView);
			this.Name = "RabatDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKategorijaBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKlijentBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDProizvodacBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				RabatDataGridViewEventArgs args = new RabatDataGridViewEventArgs();
				args.Rabat = _currentRabat;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class RabatDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Rabat	_Rabat;
	
			/// <summary>
			/// the  Entities.Rabat instance.
			/// </summary>
			public Entities.Rabat Rabat
			{
				get { return _Rabat; }
				set { _Rabat = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxRabatDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnRabatDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxRabatDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRabatDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRabatDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = RabatList[e.RowIndex].ID;
						break;
					case "uxIDKlijentDataGridViewColumn":
						e.Value = RabatList[e.RowIndex].IDKlijent;
						break;
					case "uxIDProizvodacDataGridViewColumn":
						e.Value = RabatList[e.RowIndex].IDProizvodac;
						break;
					case "uxIDKategorijaDataGridViewColumn":
						e.Value = RabatList[e.RowIndex].IDKategorija;
						break;
					case "uxVrijednostDataGridViewColumn":
						e.Value = RabatList[e.RowIndex].Vrijednost;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxRabatDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRabatDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRabatDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						RabatList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxIDKlijentDataGridViewColumn":
						RabatList[e.RowIndex].IDKlijent = (System.Int32?)e.Value;
						break;
					case "uxIDProizvodacDataGridViewColumn":
						RabatList[e.RowIndex].IDProizvodac = (System.Int32?)e.Value;
						break;
					case "uxIDKategorijaDataGridViewColumn":
						RabatList[e.RowIndex].IDKategorija = (System.Int32?)e.Value;
						break;
					case "uxVrijednostDataGridViewColumn":
						RabatList[e.RowIndex].Vrijednost = (System.Single?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
